"""
爬取豆瓣新片榜 https://movie.douban.com/chart
电影的名字
电影的主演
电影的评分
电影的评论量
电影的链接

"""
import requests
from lxml import etree
import csv

# 构建user-agent
headers= {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
}
url = "https://movie.douban.com/chart"
# 发送请求 得到响应
res = requests.get(url,headers=headers)
movie_data = res.content.decode()

# 转换为html文档
movie_data_html = etree.HTML(movie_data)
# 从movie_data_html中提取有用的数据
movie_list = movie_data_html.xpath("//div[@class='pl2']")
# 构建列表，可以遍历写入csv文件，每次遍历，写入一行数据
data = [["movie_name","movie_actor","movie_score","movie_comment","movie_path"]]
for i in movie_list:
    new_list = []
    # 电影的名字
    name = i.xpath("./a/text()")[0].replace("/"," ").strip()
    # 电影的主演
    yanyuan = i.xpath("./p/text()")[0]
    index = yanyuan.find("/")
    actor_data = yanyuan[index+1:]
    # 电影的评分
    score = i.xpath(".//span[@class='rating_nums']/text()")[0]
    # 电影的评论量
    comment = i.xpath("./div/span[3]/text()")[0]
    # 电影的链接
    path = i.xpath("./a/@href")[0]
    # 一次性将所有数据添加到列表中new_list
    new_list.extend([name,actor_data,score,comment,path])
    # 将小列表new_list追加到大列表data中
    data.append(new_list)


# 遍历data 每遍历一次写一行
for row in data:
    with open("movie.csv","a",encoding='gbk',newline="") as f:
        # csv.writer() 第一个参数：文件对象 第二个参数：类型
        csv_writer = csv.writer(f,dialect="excel",)
        # 每次写一行数据
        csv_writer.writerow(row)



